home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / PROGRAMG / GRAD / GRAD.DOC < prev    next >
Text File  |  1993-01-07  |  10KB  |  203 lines

  1.             GRAD: AUTOMATIC DIFFERENTIATION OF FORTRAN PROGRAMS
  2.  
  3.                         USER NOTES  -  January 1993
  4.  
  5.  
  6. INTRODUCTION
  7.  
  8. The computation of partial derivatives is required in optimization, 
  9. parameter estimation, sensitivity analysis, and other problems.  With 
  10. complicated functions, the coding of derivative computations can be 
  11. laborious and error-prone and, in many instances, impractical.  When the 
  12. number of variables is large, the alternative of using finite difference 
  13. approximations is computationally costly, with many function evaluations 
  14. required to compute a gradient.  Symbolic algebra packages can sometimes be 
  15. used to generate the derivatives.  These packages may be inadequate, 
  16. however, when the functions to be differentiated are defined by computer 
  17. programs including intermediate variables, loops, and conditionals. 
  18.  
  19. The system described here, GRAD, does most of the work of generating a 
  20. Fortran subprogram for computing the gradient, given a Fortran subrprogram 
  21. that computes the function.  It has been used successfully since 1988 for 
  22. maximum-likelihood estimation in complex statistical models (García 1989, 
  23. 1992).  GRAD has been implemented in the APL computer language, but no
  24. knowledge of APL is required for its use.  Methods and implementation are
  25. described elsewhere (García 1992; a file with part of a 1991 unpublished
  26. report is included with this release). 
  27.  
  28.  
  29. SYSTEM REQUIREMENTS
  30.  
  31. This release contains a stand-alone executable file, GRAD.EXE, for
  32. IBM-compatible PC's.  There are no special hardware requirements.
  33.  
  34. This is a port to IBM's APL2/PC of the STSC APL*PLUS version, converted to
  35. a stand-alone executable with the APL2/PC packaging facility.  No attempt
  36. was made to make use of the APL2 extended language features.  The sources
  37. for this and the previous APL*PLUS and I-APL versions are available on
  38. request.
  39.  
  40.  
  41. FUNCTION CODING
  42.  
  43. The function to be differentiated is usually given by a Fortran SUBROUTINE 
  44. or FUNCTION subprogram.  It is also possible to process arbitrary Fortran 
  45. code segments.  The following restrictions must be observed in the coding 
  46. of the source function:  
  47.  
  48. a) The independent variables must be scalars or array elements with 
  49. constant subscripts.  Array elements with variable subscripts, such as 
  50. X(I), may be misinterpreted without warning.  Also, the independent 
  51. variables must not be re-assigned (i.e. must not appear on the left-hand-
  52. side of an assignment statement).  If necessary, you may introduce 
  53. auxilliary variables to work around these restrictions. 
  54.  
  55. b) Values involving the independent variables directly or indirectly (i.e. 
  56. with non-zero partial derivatives) must be altered only by assignment 
  57. statements.  In particular, variable changes resulting from EQUIVALENCE, 
  58. READ and CALL statements are ignored without warning. 
  59.  
  60. c) Assignments involving the independent variables directly or indirectly 
  61. are not allowed in IF statements.  An error message is produced. 
  62.  
  63. d) Labels are not allowed in assignment statements involving the 
  64. independent variables directly or indirectly (use CONTINUE).  An error 
  65. message is produced. 
  66.  
  67. e) Calling user-defined functions or FUNCTION subprograms involving the 
  68. independent variables directly or indirectly is not supported, and they 
  69. will be misinterpreted without warning. 
  70.  
  71. f) The following functions are supported:  DABS, DLOG, DEXP, DSQRT, DSIN, 
  72. DCOS, DTAN, DASIN, DACOS, and DATAN.  The single-precision forms (ABS, 
  73. ALOG, etc., but not LOG) are also accepted, but the double-precision 
  74. version is used in the output, and a warning is produced.  The names must 
  75. be in upper-case.  Other functions are ignored without warning. 
  76.  
  77. g) The COMPLEX data type is not supported.
  78.  
  79. h) Upper- or lower-case must be used consistenly in variable identifiers.  
  80. That is, a variable name must be always in upper-case or always in lower-
  81. case (or have always the same mix of upper- and lower-case). 
  82.  
  83. It is assumed that the input is valid Fortran, i.e., it has been compiled 
  84. with no errors. 
  85.  
  86.  
  87. RUNNING GRAD
  88.  
  89. Enter "GRAD" from DOS (do not type the quotes).  Then answer the screen
  90. prompts.  The first prompt is for the input file name (the file with the
  91. Fortran subprogram source to be differentiated).  Path names (e.g.
  92. "D:\WRK\FUNC1.FOR") are acceptable (try the examples FUNC1.FOR, FUNC2.FOR
  93. and FUNC3.FOR included with GRAD).  The second prompt is for an output file
  94. name, where the output routine will be stored. 
  95.  
  96. The next prompt is for the derivative index delimiter.  The identifiers for 
  97. the intermediate derivatives are formed by appending this delimiter and the 
  98. number of the independent variable to the identifiers on the left-hand-side 
  99. of assignments.  Press Enter to accept the default ("_", unless changed in 
  100. a previous run in this session).  If the default is not acceptable to your 
  101. compiler, or you want a different delimiter for any reason (e.g. for second 
  102. derivatives, see below), type it in and press Enter.  Note that in the APL 
  103. keyboard symbol locations may differ from those in the standard keyboard.  
  104. Warning: if a GRAD-generated name happens to coincide with an existing name 
  105. the results will be incorrect.  
  106.  
  107. Finally, you must enter the names of the independent variables, separated 
  108. by "\" (spaces are not significant).  For the examples: 
  109.         FUNC1:  X\Y
  110.         FUNC2:  X\Y\Z
  111.         FUNC3:  X(1)\X(2)\X(3)\X(4)
  112. If you require more than one screen line, end the line with a "\" which
  113. will be interpreted as a continuation character.
  114.  
  115. GRAD starts then to output the code for the derivatives, both to the screen
  116. and to the output file.  When finished, the message "Done" is displayed.  
  117. GRAD may terminate with an error message (possibly unintelligible) if there
  118. are file errors or invalid Fortran statements.
  119.  
  120. MS-DOS pipes and input/output re-direction (use of |, <, and > on the
  121. command line) are supported.
  122.  
  123. Second or higher derivatives can be generated simply by running the output 
  124. through GRAD.  Different delimiter characters for the identifiers must be 
  125. used in each run.  The resulting code, however, will perform redundant 
  126. computations, since (for second derivatives) two versions of the gradient 
  127. and of the off-diagonal Hessian elements are generated.  It would not be 
  128. difficult to eliminate this redundancy in a post-processing step, but this 
  129. has not been implemented.  You might be able to do this with a text editor
  130. macro, or a short program in AWK. 
  131.  
  132.  
  133. OUTPUT
  134.  
  135. You must edit the header of the output routine to change its name and to 
  136. include the derivatives as arguments.  For example, suppose that the input 
  137. header was 
  138.  
  139.       SUBROUTINE FUNC1(X, Y, Z, F) ,
  140.  
  141. where X, Y and Z are the independent variables, and F is the function 
  142. value.  Then the partial derivatives are assigned to the new variables F_1, 
  143. F_2 and F_3 just before the final assignment to F (assuming that the 
  144. default delimiter was used).  It is then necessary to change the header to 
  145. something like 
  146.  
  147.       SUBROUTINE DERS1(X, Y, Z, F, F_1, F_2, F_3) .
  148.  
  149. If default types or IMPLICIT declarations are not used, it may also be 
  150. necessary to declare the new variables (including intermediate ones). 
  151.  
  152. You must dimension any arrays created by GRAD.  These correspond to 
  153. derivatives of arrays in the source routine, and must have the same 
  154. dimension. 
  155.  
  156. Error messages are included in the output in lines starting with " ***".  
  157. If ignored, they will be detected by the compiler. 
  158.  
  159. Two types of warnings, starting with "C*** WARNING:", may be produced.  
  160. They may be left in the code as comments.  The first is "identifier too 
  161. long", and is produced when identifiers longer than the Fortran standard of 
  162. 6 characters are generated.  Many compilers provide the option of accepting 
  163. long identifiers (for example with the switch /4Nt in Microsoft Fortran).  
  164. If your compiler does not, you would have to change those names with an 
  165. editor. 
  166.  
  167. The other warning, "statement interpreted as...", indicates a discrepancy 
  168. between the source statement and the result of its parsing by GRAD.  This 
  169. is a safety feature, and should be triggered only by redundant parenthesis, 
  170. and by single-precision function names which are converted to their double-
  171. precision form for output. 
  172.  
  173. You may see in the output some ackward-looking expressions, such as
  174. X**(2-1).  Do not worry, these are taken care off by the optimizer in any 
  175. decent Fortran compiler. 
  176.  
  177. Some logical or character assignments may generate superfluous, but 
  178. harmless code. 
  179.  
  180. If possible, it is recommended to check the generated subroutine by 
  181. comparison against finite differences. 
  182.  
  183.  
  184. REFERENCES
  185.  
  186. García, O. 1989.  Growth Modelling - New developments.  In: Nagumo, H. and 
  187.     Konohira, Y. (Ed.)  Japan and New Zealand Symposium on Forestry 
  188.     Management Planning.  Japan Association for Forestry Statistics. 
  189.  
  190. García, O. 1992.  A system for the differentiation of Fortran code and an
  191.     application to parameter estimation in forest growth models.  In:
  192.     Griewank, A., and Corliss, G.F. (eds)  Automatic differentiation of
  193.     algorithms:  theory, implementation and application.  Society for
  194.     Industrial and Applied Mathematics, Philadelphia.
  195.  
  196.  
  197. Oscar García
  198. Instituto Forestal
  199. Huerfanos 554
  200. Santiago                  E-mail:   ogarcia@uchcecvm.bitnet
  201. CHILE                     Fax:      +56(2)638-1286
  202.  
  203.